package com.climb.moive.controller;

import com.climb.moive.entity.Member;
import com.climb.moive.entity.MovieEvaluation;
import com.climb.moive.entity.MovieReview;
import com.climb.moive.framework.mvc.BaseController;
import com.climb.moive.service.MemberService;
import com.climb.moive.service.MovieEvalService;
import com.climb.moive.service.MovieReviewService;
import com.climb.moive.service.impl.MemberServiceImpl;
import com.climb.moive.service.impl.MoiveEvalServiceImpl;
import com.climb.moive.service.impl.MovieReviewServiceImpl;
import com.climb.moive.util.CommmonUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author Administrator
 * 普通会员-用户个人中心
 */
@WebServlet("/memberCenter")
public class MemberCenterController extends BaseController {
    MemberService memberService=new MemberServiceImpl();
    MovieEvalService movieEvalService=new MoiveEvalServiceImpl();
    MovieReviewService movieReviewService=new MovieReviewServiceImpl();
    @Override
    public String index(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session= req.getSession();

        if (session.getAttribute("member") != null) {
            /*用户个人信息*/
            Member member=(Member)session.getAttribute("member");
            req.setAttribute("member",member);
            /*用户影评文章movieReviewEssayList*/
            List<MovieReview> movieReviews=movieReviewService.findReviewByMemberId(member.getId());
            req.setAttribute("movieReviewEssayList",movieReviews);
            /*用户评论movieEvaluationList*/
            List<MovieEvaluation> movieEvaluations=movieEvalService.findEvaluaByMemberId(member.getId());
            req.setAttribute("movieEvaluationList",movieEvaluations);
            return "forward:/view/member/MemberCenter.jsp";
        } else {
            return "redirect:"+req.getContextPath()+"/memberIndex?method=doLogin";
        }

    }
    /**修改个人中心*/
    public String modify(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Member member=new Member();
        /*装载对象*/
        CommmonUtils.parseBean(member,req.getParameterMap());
        /*保存更改对象信息*/
        memberService.update(member);
        req.setAttribute("member",memberService.findMemberById(member.getId()));
        /*刷新session* 里面的对象*/
        HttpSession session= req.getSession();
        session.setAttribute("member",memberService.findMemberById(member.getId()));
        /*重新返回该地址*/
        return "forward:/view/member/MemberCenter.jsp";
    }

}
